package battleships;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.AbstractListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.SpinnerListModel;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;




public class gui extends JFrame implements ActionListener{

		/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		
	
	
		private JPanel contentPane;
		private JTable table;
		private JTable table_1;
		private JButton  btnFire;
		private JButton btnStartGame;
		
		Deployment newGame;
		ArrayList positions;
		
		static String[] Home_pose = new String[100];
		static String[] Fire_pose = new String[100];
		

		/**
		 * Launch the application.
		 */
		public static void main(String[] args) {
			sound.miss.play();
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						for(int x = 0; x<100; x++){
							Home_pose[x] = null;
							Fire_pose[x] = null;
						}
						gui frame = new gui();
						frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		
		
		
		}

		/**
		 * Create the frame.
		 */
		
		
		public gui() {
			
			
			
			
			setTitle ("Battleship");
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 50, 1050, 700 );
			
			JMenuBar menuBar = new JMenuBar();
			setJMenuBar(menuBar);
			
			JMenu mnFile = new JMenu ("File");		//Add file to Menu Bar
			mnFile.setMnemonic(KeyEvent.VK_F);
			menuBar.add(mnFile);
			
			//Add to the File Tab
			JMenuItem mnItemNewGame = new JMenuItem("New Game");
			mnFile.add(mnItemNewGame);
			
			JMenuItem mnItemSave = new JMenuItem("Save");
			mnFile.add(mnItemSave);
			
			JMenuItem mnItemOpen = new JMenuItem("Open");
			mnFile.add(mnItemOpen);
			
			JMenuItem mnItemExit = new JMenuItem("Exit");
			mnFile.add(mnItemExit);
			
			//Add player to menu Bar
			JMenu mnPlayer = new JMenu ("Player");
			mnPlayer.setMnemonic(KeyEvent.VK_P);
			menuBar.add(mnPlayer);
			
			//Add to the player tab
			JMenuItem mnItemHighScore = new JMenuItem("High Score");
			mnPlayer.add(mnItemHighScore);
			
			JMenuItem mnItemStats = new JMenuItem("Stats");
			mnPlayer.add(mnItemStats);
			
			//Add Help to Menu Bar
			JMenuItem mnHelp = new JMenu ("Help");
			mnHelp.setMnemonic(KeyEvent.VK_H);
			menuBar.add(mnHelp);
			
			//Add to Help tab
			JMenuItem mnIteminfo = new JMenuItem("Info");
			mnHelp.add(mnIteminfo);
			
			
			contentPane = new JPanel();
			contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
			setContentPane(contentPane);
			
			
			
			//A-J spinner
			JSpinner spinner = new JSpinner();
			spinner.setModel(new SpinnerListModel(new String[] {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}));
			
			//0-10 spinner
			JSpinner spinner_1 = new JSpinner();
			spinner_1.setModel( new SpinnerListModel(new String []{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}));
			
			
			
			JButton btnFire = new JButton("Fire");
			
			JButton btnStartGame = new JButton("Start Game");
			
			
			
			
			
			
			
			
			fireSquence c1 = new fireSquence();
			// when fire button is clicked start fire squence method
			btnFire.addActionListener(c1);
			// when start button is clicked start fire squence method
			btnStartGame.addActionListener(c1);
			
			
		
			
			
			table = new JTable();
			table.setRowHeight(32);
			table.setBackground(Color.getHSBColor(45, 95,250));
			table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			table.setFillsViewportHeight(true);
			table.setModel(new DefaultTableModel(
				new Object[][] {
						{null, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"},
						{"A", Fire_pose[0], Fire_pose[1], Fire_pose[2], Fire_pose[3], Fire_pose[4], Fire_pose[5], Fire_pose[6], Fire_pose[7], Fire_pose[8], Fire_pose[9]},
						{"B", Fire_pose[10], Fire_pose[11], Fire_pose[12], Fire_pose[13], Fire_pose[14], Fire_pose[15], Fire_pose[16], Fire_pose[17], Fire_pose[18], Fire_pose[19]},
						{"C", Fire_pose[20], Fire_pose[21], Fire_pose[22], Fire_pose[23], Fire_pose[24], Fire_pose[25], Fire_pose[26], Fire_pose[27], Fire_pose[28], Fire_pose[29]},
						{"D", Fire_pose[30], Fire_pose[31], Fire_pose[32], Fire_pose[33], Fire_pose[34], Fire_pose[35], Fire_pose[36], Fire_pose[37], Fire_pose[38], Fire_pose[39]},
						{"E", Fire_pose[40], Fire_pose[41], Fire_pose[42], Fire_pose[43], Fire_pose[44], Fire_pose[45], Fire_pose[46], Fire_pose[47], Fire_pose[48], Fire_pose[49]},
						{"F", Fire_pose[50], Fire_pose[51], Fire_pose[52], Fire_pose[53], Fire_pose[54], Fire_pose[55], Fire_pose[56], Fire_pose[57], Fire_pose[58], Fire_pose[59]},
						{"G", Fire_pose[60], Fire_pose[61], Fire_pose[62], Fire_pose[63], Fire_pose[64], Fire_pose[65], Fire_pose[66], Fire_pose[67], Fire_pose[68], Fire_pose[69]},
						{"H", Fire_pose[70], Fire_pose[71], Fire_pose[72], Fire_pose[73], Fire_pose[74], Fire_pose[75], Fire_pose[76], Fire_pose[77], Fire_pose[78], Fire_pose[79]},
						{"I", Fire_pose[80], Fire_pose[81], Fire_pose[82], Fire_pose[83], Fire_pose[84], Fire_pose[85], Fire_pose[86], Fire_pose[87], Fire_pose[88], Fire_pose[89]},
						{"J", Fire_pose[90], Fire_pose[91], Fire_pose[92], Fire_pose[93], Fire_pose[94], Fire_pose[95], Fire_pose[96], Fire_pose[97], Fire_pose[98], Fire_pose[99]},
					},
				new String[] {
					"New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column", "New column"
				}
			));
			table.setCellSelectionEnabled(true);
			table.setColumnSelectionAllowed(true);
			table.setRowSelectionAllowed(true);
			
			table_1 = new JTable();
			table_1.setEnabled(false);
			table_1.setBackground(Color.getHSBColor( 35, 95,250));
			table_1.setRowHeight(32);
			table_1.setModel(new DefaultTableModel(
				new Object[][] {
					{null, "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"},
					{"A", Home_pose[0], Home_pose[1], Home_pose[2], Home_pose[3], Home_pose[4], Home_pose[5], Home_pose[6], Home_pose[7], Home_pose[8], Home_pose[9]},
					{"B", Home_pose[10], Home_pose[11], Home_pose[12], Home_pose[13], Home_pose[14], Home_pose[15], Home_pose[16], Home_pose[17], Home_pose[18], Home_pose[19]},
					{"C", Home_pose[20], Home_pose[21], Home_pose[22], Home_pose[23], Home_pose[24], Home_pose[25], Home_pose[26], Home_pose[27], Home_pose[28], Home_pose[29]},
					{"D", Home_pose[30], Home_pose[31], Home_pose[32], Home_pose[33], Home_pose[34], Home_pose[35], Home_pose[36], Home_pose[37], Home_pose[38], Home_pose[39]},
					{"E", Home_pose[40], Home_pose[41], Home_pose[42], Home_pose[43], Home_pose[44], Home_pose[45], Home_pose[46], Home_pose[47], Home_pose[48], Home_pose[49]},
					{"F", Home_pose[50], Home_pose[51], Home_pose[52], Home_pose[53], Home_pose[54], Home_pose[55], Home_pose[56], Home_pose[57], Home_pose[58], Home_pose[59]},
					{"G", Home_pose[60], Home_pose[61], Home_pose[62], Home_pose[63], Home_pose[64], Home_pose[65], Home_pose[66], Home_pose[67], Home_pose[68], Home_pose[69]},
					{"H", Home_pose[70], Home_pose[71], Home_pose[72], Home_pose[73], Home_pose[74], Home_pose[75], Home_pose[76], Home_pose[77], Home_pose[78], Home_pose[79]},
					{"I", Home_pose[80], Home_pose[81], Home_pose[82], Home_pose[83], Home_pose[84], Home_pose[85], Home_pose[86], Home_pose[87], Home_pose[88], Home_pose[89]},
					{"J", Home_pose[90], Home_pose[91], Home_pose[92], Home_pose[93], Home_pose[94], Home_pose[95], Home_pose[96], Home_pose[97], Home_pose[98], Home_pose[99]},
				},
				new String[] {
					"", "", "", "", "", "", "", "", "", "", ""
				}
			));
			
			JLabel lblFiringGrid = new JLabel("Firing Grid");
			
			JLabel lblHomeGrid = new JLabel("Home Grid");
			
			JList list = new JList();
			list.setModel(new AbstractListModel() {
				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;
				String[] values = new String[] {"Destroyer", "Partrol Boat", "Submarine", "Carrier", "Battleship"};
				public int getSize() {
					return values.length;
				}
				public Object getElementAt(int index) {
					return values[index];
				}
			});
			
			
			list.setName("");
			list.setDragEnabled(true);
			
			
			JLabel lblShips = new JLabel("Ships");
			GroupLayout gl_contentPane = new GroupLayout(contentPane);
			gl_contentPane.setHorizontalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_contentPane.createSequentialGroup()
						.addContainerGap(50, 50)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
							.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
								.addComponent(btnFire, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)// Fire button placement
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(spinner, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)//spinner placement
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(spinner_1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE))//spinner_1 placement
							.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
								.addComponent(table, GroupLayout.PREFERRED_SIZE, 345, GroupLayout.PREFERRED_SIZE)//Firing placement
								.addGap(100)
								.addComponent(table_1, GroupLayout.PREFERRED_SIZE, 345, GroupLayout.PREFERRED_SIZE)))//Home placement
						.addGap(100)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
							.addComponent(list, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)//Ship List
							.addComponent(btnStartGame))
						.addContainerGap(101, Short.MAX_VALUE))
					.addGroup(gl_contentPane.createSequentialGroup()
						.addGap(88)
						.addComponent(lblFiringGrid)
						.addGap(500)
						.addComponent(lblHomeGrid)
						.addPreferredGap(ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
						.addComponent(lblShips)
						.addGap(50))
			);
			gl_contentPane.setVerticalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_contentPane.createSequentialGroup()
						.addGap(50)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblFiringGrid)
							.addComponent(lblHomeGrid)
							.addComponent(lblShips))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
							.addComponent(list, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGroup(Alignment.LEADING, gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(table, 150, 150, Short.MAX_VALUE)
								.addComponent(table_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
						.addPreferredGap(ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnFire)
							.addComponent(spinner, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(spinner_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(btnStartGame))
						.addGap(25))
			);
			contentPane.setLayout(gl_contentPane);
			contentPane.setBackground(Color.LIGHT_GRAY);
		}
			
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
		
		private class fireSquence implements ActionListener{
			
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == btnFire);
					sound.fire.play();
					if (e.getSource() == btnStartGame){
					sound.miss.play();
					System.out.println("Moew");
					}
			 }
			 }
			
		
		public  void fire(ActionEvent e){
			
			sound.miss.play();
		}
		
		
		public void deploy() {
		
		
			
		}

		@Override
		public void actionPerformed(ActionEvent ae) {
			if(ae.getActionCommand() == "Start"){
				newGame = new Deployment();
				positions = newGame.GetPositions();
				setShipPosition();				
			}
			
			
		}
		
		public void setShipPosition(){
			String pose = null;
			pose = positions.toString();
			
			 Scanner sc = new Scanner(pose);
			 int x = 0;
			 int y = 0;
			 while(sc.hasNext()){

				 x = sc.nextInt();
				 y = sc.nextInt();
				 x = x + (y*10);
				 Home_pose[x] = "o";
				 x = 0; y = 0;
			 }
			 
		}

		
	}